/**
 * 初始化桌面布局
 * @author 赵振华
 */
(function(_,$){
	var global_body = null;
	var global_wallpaper = null;
	var global_wallpaper_screen = null;
	
	var global_desktop = null;
	var global_desktop_height = 0;
	
	var global_taskbar = null;
	var global_start_button = null;
	var global_item_container = null;
	var global_start_menu;
	
	var global_window = null;
	var global_window_height = 0;
	var global_window_width = 0;
	var _this_desktop_obj;
	
	_.desktop = {
		init : function(){
			global_window = $(window);
			global_window_height = global_window.height();
			global_window_width = global_window.width();
			global_body = $("body");
			global_wallpaper = global_body.children(".wallpaper");
			global_wallpaper_screen = global_wallpaper.children(".screen");
			global_desktop = global_body.children(".desktop");
			global_taskbar = global_body.children(".taskbar");
			global_start_menu = global_taskbar.children(".start_menu");
			var taskbarContent = global_taskbar.children(".content");
			global_start_button = taskbarContent.children(".start_button");
			global_item_container = taskbarContent.children(".item_container");
		},
		initUI : function(){
			global_window_height = global_window.height();
			global_window_width = global_window.width();
			
			global_desktop_height = global_window_height - global_taskbar.height();
			global_desktop.height(global_desktop_height);	//桌面的大小
			global_desktop.children(".screen").height(global_desktop_height);
			
			_this_desktop_obj.taskbar.initUI();
		},
		closeContextmmenu:function(){
			var allmenu = $(".jdmer_comtextmenu");
			allmenu.each(function(i){
				var menu = allmenu.eq(i);
				var jdmenu = _(menu);
				if(jdmenu){
					jdmenu.vanish();
				}
			});
		},
		initEvent : function(){
			var _this = this;
			_this_desktop_obj.switcher.initEvent();
			_this_desktop_obj.taskbar.initEvent();
			try{
				global_wallpaper_screen.each(function(i){
					var curScreen = global_wallpaper_screen.eq(i);
					var background = curScreen.attr("background");
					if(background){
						curScreen.background(eval("("+background+")"));
					}
					curScreen.removeAttr("background");
				});
			}catch(e){}
			//禁止右键
			$(document).bind("contextmenu",function(){
				return false;
			}).click(function(){
				_this.taskbar.hideStartMenu();
				_this.closeContextmmenu();
			});
			
			
			var cm = _.contextmenu({
				menuArray : [{
					label:"排列图标",
					callback:function(_menu,_cm){
						_.Mger.alert(_cm.label(_menu.attr("id")));
					}
				},{
					label:"新建",
					child : [{
						label : "图标",
						callback:function(_menu,_cm){
							_.Mger.alert(_cm.label(_menu.attr("id")));
						}
					},{
						label : "文件夹",
						callback:function(_menu,_cm){
							_.Mger.alert(_cm.label(_menu.attr("id")));
						}
					}]
				},{
					mark:true
				},{
					id : "desktop_prev",
					label:"上一个桌面",
					iconClass:"contextmenu_icon_prev",
					callback:function(_menu,_cm){
						var index = _.desktop.currentScreenIndex - 1;
						if(index > 0){
							_.desktop.switcher.switchScreen(index);	//默认显示第一屏
						}
					}
				},{
					id : "desktop_next",
					label:"下一个桌面",
					iconClass:"contextmenu_icon_next",
					callback:function(_menu,_cm){
						var index = _.desktop.currentScreenIndex + 1;
						if(index < 6){
							_.desktop.switcher.switchScreen(index);	//默认显示第一屏
						}
					}
				},{
					mark:true
				},{
					label:"关于jDreamer",
					callback:function(_menu,_cm){
						_.Mger.alert("jDreamer是一个自由的Web开发框架。");
					}
				},{
					label:"更新记录",
					callback:function(_menu,_cm){
						var w = $(window);
						var height = w.height();
						var width = w.width();
						var win_note = _.window({
							id:"jdmer_update_note_window",
							resizable : false,	//不可以改变大小
							maxable:false,
							minable:false,
							top:(height - 400)/2,
							left : (width - 600)/2,
							title : "更新记录 - 此窗口已禁止重置大小、最大化、最小化",
							url : "page/update.html"
						});
						win_note.display();
					}
				},{
					label:"属性",
					disable:true
				},{
					label:"外观设置",
					callback:function(_menu,_cm){
						var w = $(window);
						var height = w.height();
						var width = w.width();
						var win_skinchooser = _.window({
							id:"jdmer_skinchooser_window",
							resizable : false,	//不可以改变大小
							maxable:false,
							minable:false,
							width : 400,
							height : 300,
							top:(height - 300)/2,
							left : (width - 400)/2,
							title : "皮肤选择",
							url : "page/skinchooser.html"
						});
						win_skinchooser.display();
					}
				}]
			});
			global_desktop.bind("contextmenu",function(e){
				cm.top(e.pageY).left(e.pageX).display();
			});
		},
		taskbar : {
			menu_show : false,
			menu_show_timeout : false,
			initUI : function(){
				global_taskbar.css("top",global_desktop_height);	//任务栏的位置
				global_item_container.width(global_taskbar.width() - global_start_button.width());
			},
			initEvent : function(){
				var _this = this;
				var inductor = global_start_button.children(".inductor");
				var button_bg = global_start_button.children(".button_bg");
				inductor.mouseover(function(){
					button_bg.addClass("button_bg_hover");
				}).mouseout(function(){
					button_bg.removeClass("button_bg_hover");
					if(global_start_menu.is(":hidden")){
						button_bg.removeClass("button_bg_pressed");
					}
				}).mousedown(function(){
					button_bg.addClass("button_bg_pressed");
					if(!_this.menu_show){
						global_start_menu.fadeIn(300);
						clearTimeout(_this.menu_show_timeout);
						_this.menu_show_timeout = setTimeout(function(){
							_this.menu_show = true;
						},300);
					}else{
						global_start_menu.fadeOut(300);
						clearTimeout(_this.menu_show_timeout);
						_this.menu_show_timeout = setTimeout(function(){
							_this.menu_show = false;
							button_bg.removeClass("button_bg_pressed");
						},300);
					}
				}).click(function(){
					_.desktop.closeContextmmenu();
					return false;
				});
				global_start_menu.click(function(){
					_.desktop.closeContextmmenu();
					return false;
				});
			},
			/**
			 * 隐藏开始菜单
			 */
			hideStartMenu : function(){
				var _this = this;
				var button_bg = global_start_button.children(".button_bg");
				global_start_menu.fadeOut(300);
				clearTimeout(_this.menu_show_timeout);
				_this.menu_show_timeout = setTimeout(function(){
					_this.menu_show = false;
					button_bg.removeClass("button_bg_pressed");
				},300);
			},
			
			addItem : function(_win){
				var _this = this;
				var win = _win;
				var currentContainer = global_item_container.children("[index='"+_.desktop.currentScreenIndex+"']");
				var item = $("<div id='"+win.uid+"' class='item'>"+win.title()+"</div>");
				currentContainer.append(item);
				item.mouseoverout("item_hover");
				this.switchItem(win.uid);
				item.click(function(){
					if(win.jQuery.is(":hidden")){	//如果对象是不可见的
						win.re();
						if(!win.isTop()){
							win.toTop();
						}
					}else{
						if(win.isTop()){
							win.min();
						}else{
							win.toTop();
						}
					}
					_this.switchItem(win.uid);
				});
			},
			
			removeItem : function(_win){
				var currentContainer = global_item_container.children("[index='"+_.desktop.currentScreenIndex+"']");
				currentContainer.children("[id='"+_win.uid+"']").remove();
			},
			
			switchItem : function(_win){
				var currentContainer = global_item_container.children("[index='"+_.desktop.currentScreenIndex+"']");
				currentContainer.children(".item_current").removeClass("item_current");
				currentContainer.children("[id='"+_win.uid+"']").addClass("item_current");
			}
		},
		link : {
			initUI : function(){
				
			}
		},
		currentScreenIndex : 0,
		/**
		 * 分屏切换器
		 */
		switcher : {
			initEvent : function(){
				var _this = this;
				var obj = $(".switcher");
				obj.draggable({
					scroll:false,
					containment : "parent"
				});
				
				var buttons = obj.children(".buttons").children(".button");
				buttons.each(function(i){
					var curBtn = buttons.eq(i);
					curBtn.mouseoverout("button_hover");
					curBtn.click(function(){
						_this.switchScreen(curBtn.attr("index"));
					});
				});
			},
			/**
			 * 分屏切换
			 * @param _screen	切换到屏幕的索引
			 */
			switchScreen : function(_index){
				_index = Number(_index);
				var csi = _.desktop.currentScreenIndex;
				
				var oldWallScreen = global_wallpaper.children("[current='true']");
				oldWallScreen.removeAttr("current");
				var targetWallScreen = global_wallpaper.children("[index='"+_index+"']");
				var oldDesktopScreen = global_desktop.children("[current='true']");
				oldDesktopScreen.removeAttr("current");
				var targetDesktopScreen = global_desktop.children("[index='"+_index+"']");
				var oldItemContainer =  global_item_container.children("[current='true']");
				oldItemContainer.removeAttr("current");
				var targetItemContainer = global_item_container.children("[index='"+_index+"']");
				
				if(csi == 0){	//第一次显示
					global_wallpaper.children("[class='screen'][index!='"+_index+"']").hide();
					targetWallScreen.css({
						left : 0
					}).show();
					
					global_desktop.children("[class='screen'][index!='"+_index+"']").hide();
					targetDesktopScreen.css({
						left : 0
					}).show();
					
					global_item_container.children("[class='screen'][index!='"+_index+"']").hide();
					targetItemContainer.css({
						top : 0
					}).show();
				}else if(_index > csi){	//左移动
					//背景
					oldWallScreen.animate({
						left : - global_window.width()
					},function(){
						oldWallScreen.hide();
					});
					targetWallScreen.show().css({
						left : global_window.width()
					}).animate({
						left : 0
					});
					
					//桌面
					oldDesktopScreen.animate({
						left : - global_window.width()
					},function(){
						oldDesktopScreen.hide();
					});
					targetDesktopScreen.show().css({
						left : global_window.width()
					}).animate({
						left : 0
					});
					
					//任务栏 上移动
					oldItemContainer.animate({
						top : - oldItemContainer.height()
					},function(){
						oldItemContainer.hide();
					});
					targetItemContainer.show().css({
						top : targetItemContainer.height()
					}).animate({
						top : 0
					});
				}else if(_index < csi){	//右移动
					oldWallScreen.animate({
						left : global_window.width()
					},function(){
						oldWallScreen.hide();
					});
					targetWallScreen.show().appendTo(global_wallpaper).css({
						left : - global_window.width()
					}).animate({
						left : 0
					});
					
					oldDesktopScreen.animate({
						left : global_window.width()
					},function(){
						oldDesktopScreen.hide();
					});
					targetDesktopScreen.show().css({
						left : - global_window.width()
					}).animate({
						left : 0
					});
					
					//任务栏 下移动
					oldItemContainer.animate({
						top : oldItemContainer.height()
					},function(){
						oldDesktopScreen.hide();
					});
					targetItemContainer.show().css({
						top : - targetItemContainer.height()
					}).animate({
						top : 0
					});
				}else if(_index = csi){	//不做操作
					// no code
				}
				
				targetWallScreen.attr("current","true");
				targetDesktopScreen.attr("current","true");
				targetItemContainer.attr("current","true");
				
				var obj = $(".switcher");
				obj.find(".button").removeClass("button_cur");
				obj.find("[index='"+_index+"']").addClass("button_cur");
				
				_.desktop.currentScreenIndex = _index;
			}
		}
	};
	_this_desktop_obj = _.desktop;
	
})(jDreamer,jQuery);
